package com.hskn.hss.module.treemaintain.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.tlfacilities.entity.TlFacilities;
import com.hskn.hss.module.tlfacilities.mapper.TlFacilitiesMapper;
import com.hskn.hss.module.treemaintain.entity.TreeMaintain;
import com.hskn.hss.module.treemaintain.mapper.TreeMaintainMapper;
import com.hskn.hss.module.treemaintain.service.ITreeMaintainService;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.mapper.UserMapper;
import com.hskn.hss.module.userdepart.service.ISysUserDepartService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-19
 */
@Service
public class TreeMaintainServiceImpl extends ServiceImpl<TreeMaintainMapper, TreeMaintain> implements ITreeMaintainService {

    @Resource
    private TreeMaintainMapper treeMaintainMapper;
    @Resource
    private TlFacilitiesMapper facilitiesMapper;

    @Autowired
    private ISysUserDepartService sysUserDepartService;

    @Autowired
    private ISysDepartService sysDepartService;
    @Resource
    private UserMapper userMapper;

    @Override
    public JSONObject mList(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        JSONObject json = new JSONObject();
        QueryWrapper<TreeMaintain> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(TreeMaintain::getCreateTime);
        String treeId = params.getString("treeId");
        if (StringUtils.isNotEmpty(treeId)) {
            queryWrapper.lambda().eq(TreeMaintain::getTreeId, treeId);
        }
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.lambda().between(TreeMaintain::getCreateTime, startDate, endDate);
        }
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<TreeMaintain> treeMaintainPage = new Page<>(page, size, true);
        IPage<TreeMaintain> iPage = treeMaintainMapper.selectPage(treeMaintainPage, queryWrapper);
        if (iPage!=null&&iPage.getPages()>0) {
            List<TreeMaintain> list = iPage.getRecords();
            List<String> ids = new ArrayList<>();
            for (TreeMaintain maintain : list) {
                ids.add(maintain.getCreateUid());
            }
            QueryWrapper  userQ=new QueryWrapper();
            userQ.in("uid",ids);
            List<User> userList=userMapper.selectList(userQ);
            for(TreeMaintain m:list){
                for(User user:userList){
                    if(m.getCreateUid().equals(user.getUid())){
                        m.setCreateName(user.getAccount());
                    }
                }
            }
        }
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("treeMaintains", iPage.getRecords());
        return json;
    }

    @Override
    public JSONObject getConserveDayNum(JSONObject params) {

        JSONObject json = new JSONObject();
        String id = params.getString("id");
        Integer conserveDayNum = treeMaintainMapper.getConserveDayNum(id);
        json.put("conserveDayNum", conserveDayNum);
        return json;
    }

    @Override
    public void add(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TreeMaintain treeMaintain = JSON.parseObject(params.toJSONString(), TreeMaintain.class);
        treeMaintain.setCreateUid(uid);
        treeMaintain.setDepid(sysUserDepartService.getDeptidByUid(uid));
        treeMaintain.setLeaderUids(sysDepartService.getLeaderUids());
        String facId=treeMaintain.getTreeId();
        TlFacilities facilities = facilitiesMapper.selectOne(new QueryWrapper<TlFacilities>().lambda().eq(TlFacilities::getId,facId));
        if (Objects.nonNull(facilities)) {
            treeMaintain.setTreeName(facilities.getFaclname());
            treeMaintain.setTypeId(facilities.getFacltypeitemid());
            treeMaintain.setTypeName(facilities.getFacltypeitemname());
        }

        treeMaintainMapper.insert(treeMaintain);
    }
}
